package com.lhr.controller;

import com.lhr.entity.User;
import com.lhr.exception.BusinessException;
import com.lhr.response.Result;
import com.lhr.response.ResultCode;
import com.lhr.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author LiHuiRu
 * @since 2023-02-21
 */
@Api(value = "系统用户模块",tags = "系统用户接口")
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @GetMapping("/getUserList")
    Result getUsers(){
        List<User> list = userService.list();
        return Result.ok().data("list",list);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个用户",notes = "通过ID查询对应的用户信息")
    public Result getUserById(@PathVariable("id") Long id){
        User user = userService.getById(id);
        if(user!=null){
            return Result.ok().data("user",user);
        }else{
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
    }
}
